package com.zhsj.OkeHomeConsumption.ui.fragment.minehome;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hjq.widget.SettingBar;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.MyActivity;
import com.zhsj.OkeHomeConsumption.common.MyApplication;
import com.zhsj.OkeHomeConsumption.empty.VersionInfo;
import com.zhsj.OkeHomeConsumption.utils.LogUtils;
import com.zhsj.OkeHomeConsumption.utils.UpdateAppUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版本信息页面
 */
public class VersionMessageActivity extends MyActivity {
    @BindView(R.id.versionname_text)
    TextView versionnameText;
    @BindView(R.id.versonnow_bar)
    SettingBar versonnowBar;

    int localVersion, serviceVersion;
    //版本信息
    VersionInfo versionInfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_versionmessage;
    }

    @Override
    protected int getTitleId() {
        return R.id.title_back;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        versionnameText.setText(UpdateAppUtils.getAPPLocalVersionName(MyApplication.getContext()));
        //检测版本
        updateApp(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.versonnow_bar)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.versonnow_bar:
                if (serviceVersion > localVersion){
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("versioninfo", versionInfo);
                    startActivity(new Intent(this, VersionUpdataActivity.class).putExtras(bundle));
                }else {
                    toast("已是最新版本");
                }
                break;
        }
    }

    /**
     * 判断版本号，更新APP
     *
     * @param context
     */
    private void updateApp(final Context context) {
        UpdateAppUtils.getAPPServerVersion(context, new UpdateAppUtils.VersionCallBack() {
            @Override
            public void callBack(final VersionInfo info) {
                versionInfo = info;
                localVersion = UpdateAppUtils.getAPPLocalVersion(MyApplication.getContext());
                serviceVersion = info.getVersion();
                LogUtils.i("mylog", "localVersion=" + localVersion + "----serviceVersion=" + serviceVersion);
                if (serviceVersion > localVersion) {
                    versonnowBar.setRightText("有新版本");
                    versonnowBar.setLeftColor(Color.parseColor("#E61212"));
                }else {
                    versonnowBar.setRightText("已是最新版本");
                    versonnowBar.setLeftColor(Color.parseColor("#061127"));
                }
            }
        });
    }
}
